/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package datos;

import dominio.Horario;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.util.LinkedList;

/**
 *
 * @author Mauro
 */
public class HorarioDatos extends BaseDeDatos{

    public HorarioDatos() {
    }
   
    public void ingresarHorario(Horario horarioAInsertar) throws SQLException {
        Connection con = BaseDeDatos.getConexion();
        CallableStatement cs = null;
        try {
            cs = con.prepareCall("{call sproc_ingresar_horario(?,?,?,?,?)}");
            cs.setInt(1, horarioAInsertar.getIdHorario());
            cs.setString(2, horarioAInsertar.getDia());
            cs.setTime(3, (Time) horarioAInsertar.getHoraInicio());
            cs.setTime(4, (Time) horarioAInsertar.getHoraFin());
            cs.setString(5, horarioAInsertar.getAsignaturas().getFirst().getSigla());
            cs.execute();
        } catch (SQLException e) {
            throw e;
        } finally {
            if (cs != null) {
                try {
                    cs.close();
                } catch (SQLException e) {
                    System.err.println("SQLException: " + e.getMessage());
                }
            }
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException e) {
                    System.err.println("SQLException: " + e.getMessage());
                }
            }
        }
    }//Fin
    
    public int ObtenerIdHorario() throws SQLException{
    String sqlSelect="SELECT * FROM horario";
    Connection conexion=BaseDeDatos.getConexion();
    PreparedStatement ps=conexion.prepareCall(sqlSelect);
    LinkedList<Horario>horarios=new LinkedList<>();
    ResultSet rs=ps.executeQuery();
    while(rs.next()){
        Horario h=new Horario();
        h.setIdHorario(rs.getInt(1));
        horarios.add(h);
    }
    return horarios.getLast().getIdHorario();
    }
    
}
